package cn.doitedu.engine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;

public class DataReceiverRunnable implements Runnable{

    ServerSocket serverSocket;
    ArrayBlockingQueue<String> dataQueue;

    public DataReceiverRunnable() throws IOException {
        this.serverSocket = new ServerSocket(9090);

        this.dataQueue = StaticContextV2.getDataQueue();
    }

    @Override
    public void run() {

        while(true){
            try {
                Socket socket = serverSocket.accept();
                InputStream in = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String line;
                while(  (line = br.readLine())!=null ){
                    // 放入数据队列
                   this.dataQueue.put(line);
                }

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
}
